﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Threading;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dbgservlib;

namespace wfdbg
{
    public partial class Form_Command : ToolWindow
    {
        private IDbgService m_pDbgService;
        private int m_CommandIndex = 0;
        private List<string> m_CommandsHistory = new List<string>();

        public Form_Command(IDbgService pDbgService)
        {
            m_pDbgService = pDbgService;
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form_Command));
            InitializeComponent();
        }

        public void EnterEnable(bool bEnable)
        {
            richTextBox_Command.Enabled = bEnable;
        }
        
        private void ExecuteCommand(string Command)
        {
            m_pDbgService.Execute(Command);
            ((Form_Main)this.ParentForm).m_WStack.RefreshStack();
        }

        private void richTextBox_Command_KeyDown(object sender, KeyEventArgs e)
        {
            string Command;
            if (e.KeyData == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                Command = richTextBox_Command.Text;
                richTextBox_Command.Clear();

                if (0 == m_CommandsHistory.Count()
                    || !Command.Equals(m_CommandsHistory.Last()
                    ))
                {
                    m_CommandsHistory.Add(Command);
                }

                m_CommandIndex = m_CommandsHistory.Count() - 1;

                ExecuteCommand(Command);
            }

            if (e.KeyData == Keys.Up)
            {
                e.SuppressKeyPress = true;
                if (0 == m_CommandsHistory.Count())
                    return;

                Command = m_CommandsHistory[m_CommandIndex];
                richTextBox_Command.Text = Command;
                richTextBox_Command.Invalidate();

                if (m_CommandIndex > 0)
                    m_CommandIndex--;
            }
            if (e.KeyData == Keys.Down)
            {
                e.SuppressKeyPress = true;
                if (0 == m_CommandsHistory.Count())
                    return;

                Command = m_CommandsHistory[m_CommandIndex];
                richTextBox_Command.Text = Command;
                richTextBox_Command.Invalidate();

                if (m_CommandIndex < m_CommandsHistory.Count() - 1)
                    m_CommandIndex++;
            }
        }
    }
}
